package ctrFinanceiro.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class Persistencia {

	private static EntityManagerFactory emf;
    private static EntityManager em;

    public static void persist(Object entity) {

        configEntityManager();
        em.getTransaction().begin();

        try {
            em.persist(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public static void delete(Object entity) {

        configEntityManager();
        em.getTransaction().begin();

        try {
            
            if (!em.contains(entity)) {
                entity = managedEntity(em, entity);
            }
            
            em.remove(entity);
            em.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public static Object update(Object entity) {

        configEntityManager();
        em.getTransaction().begin();

        Object o = null;

        try {

            o = em.merge(entity);
            em.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }

        return o;
    }
    
    public static Object find(String namedQuery, Object params[]) {
        
        configEntityManager();
        Query query = em.createNamedQuery(namedQuery);
        
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                query.setParameter("p" + i, params[i]);
            }
        }

        try {

            return query.getSingleResult();

        } catch (NoResultException e) {
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            em.close();
        }
    }
    
    @SuppressWarnings("rawtypes")
	public static List getLista(String namedQuery, Object[] params) {
        
        configEntityManager();
        Query query = em.createNamedQuery(namedQuery);
        
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                query.setParameter("p" + i, params[i]);
            }
        }

        try {

            return query.getResultList();

        } catch (NoResultException e) {
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            em.close();
        }
    }
    
	@SuppressWarnings({"rawtypes","unchecked"})
    public static Object findById(Class entity, Integer id) {
        
        configEntityManager();

        try {

            return em.find(entity, id);
        
        } catch (NoResultException e) {
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            em.close();
        }
    }
	
    private static void configEntityManager() {
        emf = Persistence.createEntityManagerFactory("ctrFinanceiroPU");
        em = emf.createEntityManager();
    }
    
    private static Object managedEntity(EntityManager em, Object bean) {
        return em.merge(bean);
    }
    
	

}
